from tkinter import *
from PIL import ImageTk
import hashlib
import webbrowser

root = Tk()

text=Text(root,width=36,height=26,undo=True)
text.grid(row=0,padx=10,pady=5)

text.insert(INSERT,'i love FishC.com!\n')
text.insert(END,'i love you to\n','tag_1')
    

#打开网页，改变鼠标样式
def show_arrow(event):
    text.config(cursor='arrow')
def show_xterm(event):
    text.config(cursor='xterm')
def click(event):
    webbrowser.open('http://www.fishc.com')
#检查内容
def show():
    print('点')
def delet(x):
    if x == 'b':
        text.delete(b1)
    elif x == 'i':
        text.delete(photo)
def getmd5(content):
    m=hashlib.md5(content.encode())
    return m.digest()
def check():
    content=text.get('1.0',END)
    if sign == getmd5(content):
        print('无事发生')
    else:
        print('内容发生改变')
#撤销与恢复
def redo():
    text.edit_redo()
def undo():
    text.edit_undo()
def callback(event):
    text.edit_separator()
content  = text.get('1.0',END)        
text.tag_config("tag_1", background="yellow", foreground="red")
text.tag_add('marks','1.7','1.17')
text.tag_config("marks",  foreground="blue",underline=True)

text.tag_bind('marks','<Enter>',show_arrow)
text.tag_bind('marks','<Leave>',show_xterm)
text.tag_bind('marks','<Button-1>',click)

photo  =ImageTk.PhotoImage(file='cat_200_300.jpg')

sign = getmd5(content)
       
b1=Button(root,text='点我',command=show)
b2=Button(root,text='删除按钮',command= lambda x='b':delet(x))
b3=Button(root,text='删除图片',command= lambda x='i':delet(x))
Button(root,text='检查',command=check)\
                                      .grid(row=0,column=1,padx=10,pady=5)
text.window_create(INSERT,window=b1)
text.window_create(INSERT,window=b2)
text.window_create(INSERT,window=b3)
text.image_create(END,image=photo)

Button(root,text='恢复',command=redo)\
                                      .grid(row=0,column=2,padx=10,pady=5)
Button(root,text='撤销',command=undo)\
                                      .grid(row=0,column=3,padx=10,pady=5)
text.bind('<Key>',callback)


mainloop()
